package com.yuwei.mine.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.yuwei.base.RoutePath
import com.yuwei.common.event.BusEvent
import com.yuwei.common.event.LiveDataBus
import com.yuwei.common.mvi.BaseMviFragment
import com.yuwei.common.mvi.BaseViewFragment
import com.yuwei.common.mvi.UiState
import com.yuwei.database.entity.FavVideoEntity
import com.yuwei.mine.MineIntent
import com.yuwei.mine.MineViewModel
import com.yuwei.mine.R
import com.yuwei.mine.adapter.FavVideoAdapter
import com.yuwei.mine.databinding.FragmentLikesBinding

/**
 * 喜欢Fragment
 */
@Route(path = RoutePath.MODULE_MINE_LIKES_FRAGMENT)
class LikesFragment : BaseMviFragment<FragmentLikesBinding,MineViewModel>() {
    private val favVideoAdapter = FavVideoAdapter()

    override fun successData(it: UiState.onSuccess<*>) {
        when(it.type){
            "getFavAll" -> {
                favVideoAdapter.data.clear()
                favVideoAdapter.data.addAll(it.data as List<FavVideoEntity>)
                favVideoAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun failureData(error: String) {

    }

    override fun initView() {
        binding.favVideoRv.adapter = favVideoAdapter
        binding.favVideoRv.layoutManager = GridLayoutManager(requireContext(),3)
        vm.sendIntent(MineIntent.getFavAll)
    }

    override fun initData() {
        LiveDataBus.getDefault<BusEvent>("fav").observe(this@LikesFragment){
            vm.sendIntent(MineIntent.getFavAll)
        }
    }

    override fun onResume() {
        super.onResume()

    }


}